博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
邮件发送
阅读量:5278 次
发布时间:2019-06-14

本文共 6981 字,大约阅读时间需要 23 分钟。

1:整合springboot的发送参见:

https://blog.csdn.net/zyw_java/article/details/81635375

2:常见的邮箱服务器地址:

https://blog.csdn.net/chuanyu/article/details/46740287

3:如何查询公司的outlook邮箱服务器地址:

https://zhidao.baidu.com/question/39774692.html

 

关键点:

密码为三方授权码

待解决问题:

如何查询企业邮箱的服务地址?

 

demo

org.springframework.boot
spring-boot-starter-mail

配置

spring:  application:    name: test-config-server  profiles:  mail:    host: smtp.163.com #发送邮件服务器    username: ppyun7c@163.com #163邮箱    password: wdsqm01 #客户端授权码    protocol: smtp #发送邮件协议    properties.mail.smtp.auth: true    properties.mail.smtp.port: 465 #端口号465或994    properties.mail.display.sendmail: XIEDONGXU #可以任意    properties.mail.display.sendname: Spring Boot Guide Email #可以任意    properties.mail.smtp.starttls.enable: true    properties.mail.smtp.starttls.required: true    properties.mail.smtp.ssl.enable: true    default-encoding: utf-8    from: ppyun7c@163.com #与上面的username保持一致

 

代码

package com.test.domi.service;import javax.mail.MessagingException;public interface IMailService {    /**     * 发送文本邮件     * @param to     * @param subject     * @param content     */    public void sendSimpleMail(String to, String subject, String content, String... cc);    /**     * 发送HTML邮件     * @param to     * @param subject     * @param content     * @throws MessagingException     */    public void sendHtmlMail(String to, String subject, String content, String... cc)throws MessagingException;    /**     * 发送带附件的邮件     * @param to     * @param subject     * @param content     * @param filePath     * @throws MessagingException     */    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)throws MessagingException;    /**     * 发送正文中有静态资源的邮件     * @param to     * @param subject     * @param content     * @param rscPath     * @param rscId     * @throws MessagingException     */    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc)throws MessagingException;}

 实现类

package com.test.domi.service.impl;import com.test.domi.service.IMailService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Component;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;@Componentpublic class IMailServiceImpl implements IMailService {    @Autowired    private JavaMailSender mailSender;    @Value("${spring.mail.from}")    private String from;    /**     * 发送文本邮件     * @param to     * @param subject     * @param content     */    @Override    public void sendSimpleMail(String to, String subject, String content, String... cc) {        SimpleMailMessage message = new SimpleMailMessage();        message.setFrom(from);        message.setTo(to);        message.setCc(cc);        message.setSubject(subject);        message.setText(content);        mailSender.send(message);    }    /**     * 发送HTML邮件     * @param to     * @param subject     * @param content     */    @Override    public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException{        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        mailSender.send(message);    }    /**     * 发送带附件的邮件     * @param to     * @param subject     * @param content     * @param filePath     * @throws MessagingException     */    @Override    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)throws MessagingException {        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        FileSystemResource file = new FileSystemResource(new File(filePath));        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));        helper.addAttachment(fileName, file);        mailSender.send(message);    }    /**     * 发送正文中有静态资源的邮件     * @param to     * @param subject     * @param content     * @param rscPath     * @param rscId     */    @Override    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException {        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        FileSystemResource res = new FileSystemResource(new File(rscPath));        helper.addInline(rscId, res);        mailSender.send(message);    }}

 

 

controller

package com.test.domi.controller;import com.test.domi.common.utils.ResultInfo;import com.test.domi.common.utils.ResultUtil;import com.test.domi.service.impl.IMailServiceImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/email")public class EmailController {    @Autowired    private IMailServiceImpl mailService;//注入发送邮件的各种实现方法    @GetMapping("/normal")    public ResultInfo index(){        try {            mailService.sendSimpleMail("491431567@qq.com","SpringBoot Email","这是一封普通的SpringBoot测试邮件");            String[] cc = {"491431567@qq.com","491431567@qq.com"};            mailService.sendAttachmentsMail("491431567@qq.com","附件发送测试","这是附件","C:\\Users\\Domi\\Desktop\\ls.txt",cc);            mailService.sendHtmlMail("491431567@qq.com","html测试","\n"                    + "    
\n" +"

欢迎使用IJPay -By Javen

\n" +"
https://github.com/Javen205/IJPay" + "
IJPay 让支付触手可及,欢迎Start支持项目发展:)
\n" + "
如果对你有帮助,请任意打赏\n" + "
\n" + " \n" + "",cc); }catch (Exception ex){ ex.printStackTrace(); return ResultUtil.getFailResult("999999","失败",null); } return ResultUtil.getSuccessResult(null); }}

 

转载于:https://www.cnblogs.com/domi22/p/9749024.html

你可能感兴趣的文章
[Kaggle] Sentiment Analysis on Movie Reviews
查看>>
价值观
查看>>
mongodb命令----批量更改文档字段名
查看>>
国外常见互联网盈利创新模式
查看>>
android:scaleType属性
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>
Linux中防火墙centos
查看>>
[JS]递归对象或数组
查看>>
linux sed命令
查看>>
程序存储问题
查看>>
优雅地书写回调——Promise
查看>>
PHP的配置
查看>>
Struts框架----进度1
查看>>
Round B APAC Test 2017
查看>>
MySQL 字符编码问题详细解释
查看>>
css & input type & search icon
查看>>
C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
查看>>
语音识别中的MFCC的提取原理和MATLAB实现
查看>>